var ownerEditApp = ownerEditApp || {};
ownerEditApp.model = ownerEditApp.model || {};
ownerEditApp.view = ownerEditApp.view || {};

$(function () {
    $("#pathUp").text('业主');
    $("#pathDown").text('业主详情');
    //model
    ownerEditApp.model.owModel = Backbone.Model.extend({
        url: window.ctx + "/rest/owner/get",
        defaults: {
            id: '',
            ownerName: '',
            sex: '',
            phone: '',
            unitAdd: '',
            buildName: '',
            roomNum: '',
            homeNum: '',
            createTime: '',
            isMoney: 0,
        }
    });
    ownerEditApp.view.executeView = Backbone.View.extend({
        el: "#formDiv",

        model: new ownerEditApp.model.owModel(),

        template: _.template($("#ownerTmp").html()),

        //绑定元素
        events: {
            "click #saveBtn": "save",
            "click #backBtn": "back"
        },

        initialize: function () {
            var context = this;
            this.model.fetch({
                data: {
                    id: $("form").find("[name=id]").val()
                },
                success: function () {
                    context.render()
                },
                error: function () {
                    hintDialog("获取业主信息失败！");
                }
            });
        },
        render: function () {
            $(this.el).find("form").append(this.template(this.model.toJSON()));
            $(":text:eq(0)").focus();
        },
        save: function () {
            var context = this;
            var validate = $(context.el).find("form").validate({
                debuge: false,
                rules: {
                    ownerName: {
                        required: true,
                    },
                    phone: {
                        required: true,
                        isPhone: true,
                    },
                    unitAdd: {
                        required: false,
                    },
                    buildName: {
                        required: true,
                        remote: {
                            url: window.ctx + "/rest/build/checkBuildIn",
                            type: "post",
                            dataType: "json",
                            data: {
                                buildName: function () {
                                    return $("[name=buildName]").val();
                                },
                            },
                        },
                    },
                    roomNum: {
                        required: true,
                        number: true,
                        remote: {
                            url: window.ctx + "/rest/room/checkRoomIn",
                            type: "post",
                            dataType: "json",
                            data: {
                                buildName: function () {
                                    return $("[name=buildName]").val();
                                },
                                roomNum: function () {
                                    return $("[name=roomNum]").val();
                                },
                                id: function () {
                                    return $("[name=id]").val();
                                },
                            },
                        },
                    },
                    homeNum: {
                        required: true,
                        number: true,
                        gt: 0,
                    },
                },
                messages: {
                    ownerName: {
                        required: "业主姓名不能为空",
                    },
                    phone: {
                        required: "联系电话不能为空",
                        isPhone: "电话格式不正确",
                    },
                    unitAdd: {
                        required: "单位地址不能为空",
                    },
                    buildName: {
                        required: "楼宇号不能为空",
                        remote: "该楼宇信息不存在",
                    },
                    roomNum: {
                        required: "门户号不能为空",
                        remote: "该房间已分配给其他业主,或者不存在",
                    },
                    homeNum: {
                        required: "家庭人数不能为空",
                        gt: "家庭人数应大于0",
                    }
                },
                submitHandler: function () {
                    var param = {id: $("[name=id]").val()};
                    param["ownerName"] = $("[name=ownerName]").val();
                    param["sex"] = $("[name=sex]").val();
                    param["phone"] = $("[name=phone]").val();
                    param['unitAdd'] = $("[name=unitAdd]").val();
                    param["buildName"] = $("[name=buildName]").val();
                    param["roomNum"] = $("[name=roomNum]").val();
                    param["homeNum"] = $("[name=homeNum]").val();
                    console.log(param);
                    $.ajax({
                        url: window.ctx + "/rest/owner/save",
                        type: "post",
                        dataType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(param),
                        success: function (data) {
                            if (data) {
                                $.dialog.alert("保存业主信息成功！", function () {
                                    location.href = window.ctx + "/owner/list";
                                });
                            } else {
                                console.dir(data);
                                hintDialog("保存业主信息失败！");
                            }
                        },
                        error: function (data) {
                            hintDialog("服务器出错！");
                        }
                    });
                    return;
                }
            });
            $(context.el).find("form").submit();
        },
        back: function () {
            location.href = window.ctx + "/owner/list";
        }
    });
    new ownerEditApp.view.executeView();
});